package IViews;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class ITableView extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<String> names;
	private ArrayList<JLabel> lables;
	private ArrayList<Component> comps;
	private ArrayList<Integer> typs;
	private ArrayList<JPanel> lines;
	private IImageWraper[] border;
	private int counter = 0;

	private final JPanel content;

	private final int x;
	private final int y;
	private final int width;
	private final int entryHeight = 35;// px

	public ITableView(int pX, int pY, int pWidth) {
		x = pX;
		y = pY;
		width = pWidth;
		border = new IImageWraper[8];
		this.names = new ArrayList<String>();
		this.lables = new ArrayList<JLabel>();
		this.comps = new ArrayList<Component>();
		this.typs = new ArrayList<Integer>();
		this.lines = new ArrayList<JPanel>();
		this.setBounds(x, y, width, 23);
		this.setOpaque(false);
		this.setLayout(null);

		border[0] = new IImageWraper(0, 0, 11, 11, "Borders/corner_0.png");
		border[1] = new IImageWraper(11, 0, width - 22, 11,
				"Borders/border_1.png");
		border[2] = new IImageWraper(width - 11, 0, 11, 11,
				"Borders/corner_1.png");
		border[3] = new IImageWraper(width - 11, 11, 11, 1,
				"Borders/border_2.png");
		border[4] = new IImageWraper(width - 11, 12, 11, 11,
				"Borders/corner_2.png");
		border[5] = new IImageWraper(11, 12, width - 22, 11,
				"Borders/border_3.png");
		border[6] = new IImageWraper(0, 12, 11, 11, "Borders/corner_3.png");
		border[7] = new IImageWraper(0, 11, 11, 1, "Borders/border_0.png");
		for (int i = 0; i < border.length; i++) {
			if (border[i] != null) {
				this.add(border[i]);
			}
		}

		this.content = new JPanel();
		content.setLayout(null);
		content.setBounds(11, 11, width - 22, 1);
		content.setBackground(Color.WHITE);
		this.add(content);
	}

	private void resizeBorder() {
		border[3].changeMyImageIcon(11, this.getHeight() - 22);
		border[4].setLocation(width - 11, this.getHeight() - 11);
		border[5].setLocation(11, this.getHeight() - 11);
		border[6].setLocation(0, this.getHeight() - 11);
		border[7].changeMyImageIcon(11, this.getHeight() - 22);
		content.setSize(width - 22, this.getHeight() - 22);
	}

	public void newEntryBoolean(String name, String text, boolean defaultValue) {
		if (!names.contains(name)) {
			this.setBounds(x, y, width, this.getHeight() + entryHeight);
			this.resizeBorder();

			comps.add(new ICheckbox(width - 21 - ICheckbox.FINALWIDTH, 11
					+ (counter * entryHeight)
					+ ((entryHeight - ICheckbox.FINALHEIGHT) / 2) + 1,
					defaultValue));
			this.add(comps.get(comps.size() - 1), 0);
			// JLabel
			JLabel label = new JLabel(text);
			label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
			label.setBounds(11, 11 + (counter * entryHeight), width - 22
					- ICheckbox.FINALWIDTH, entryHeight);
			this.lables.add(label);
			this.add(label, 0);

			this.typs.add(0);
			this.names.add(name);

			++counter;
		}
	}

	public void newEntryString(String name, String text, String defaultValue) {
		if (!names.contains(name)) {
			this.setBounds(x, y, width, this.getHeight() + entryHeight);
			this.resizeBorder();

			JTextField txtF = new JTextField(defaultValue);
			txtF.setBounds(width - 11 - 200, 11 + (counter * entryHeight) + 5,
					200, 25);
			this.comps.add(txtF);
			this.add(txtF, 0);

			// JLabel
			JLabel label = new JLabel(text);
			label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
			label.setBounds(11, 11 + (counter * entryHeight), width - 22 - 200,
					entryHeight);
			this.lables.add(label);
			this.add(label, 0);

			this.typs.add(1);
			this.names.add(name);

			++counter;
		}
	}

	public void newEntryPassword(String name, String text, String defaultValue) {
		if (!names.contains(name)) {
			this.setBounds(x, y, width, this.getHeight() + entryHeight);
			this.resizeBorder();

			JPasswordField txtF = new JPasswordField(defaultValue);
			txtF.setBounds(width - 11 - 200, 11 + (counter * entryHeight) + 5,
					200, 25);
			this.comps.add(txtF);
			this.add(txtF, 0);

			// JLabel
			JLabel label = new JLabel(text);
			label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
			label.setBounds(11, 11 + (counter * entryHeight), width - 22 - 200,
					entryHeight);
			this.lables.add(label);
			this.add(label, 0);

			this.typs.add(1);
			this.names.add(name);

			++counter;
		}
	}

	public boolean entryOfBoolean(String name) {
		if (names.contains(name)) {
			int index = names.indexOf(name);
			if (typs.get(index) == 0) {
				return ((ICheckbox) comps.get(index)).getValue();
			}
		}
		new JOptionPane("Fehler beim auslesen eines Boolean aus: \"" + name
				+ "\"");
		return false;
	}

	public String entryOfString(String name) {
		if (names.contains(name)) {
			int index = names.indexOf(name);
			if (typs.get(index) == 1) {
				return ((JTextField) comps.get(index)).getText();
			} else if (typs.get(index) == 2) {
				return "" + (((JPasswordField) comps.get(index)).getPassword());
			}
		}
		new JOptionPane("Fehler beim auslesen eines String aus: \"" + name
				+ "\"");
		return "";
	}

	public void newEntryButton(String name, String text) {
		if (!names.contains(name)) {
			this.setBounds(x, y, width, this.getHeight() + entryHeight);
			this.resizeBorder();
			JPanel jp = new JPanel();
			jp.setLayout(null);
			jp.setBackground(Color.WHITE);
			jp.setBounds(11, 11 + (counter * entryHeight), width - 22,
					entryHeight);
			JLabel jl = new JLabel(text);
			Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 20);
			jl.setFont(f);
			jl.setBounds(0, 0, width - 22 - 25, entryHeight);
			jp.add(jl, 0);
			jp.add(new IImageWraper(width - 22 - 25, (entryHeight / 2) - 10,
					25, 20, "chevron.png"));

			this.add(jp, 0);
			this.comps.add(jp);
			this.lables.add(jl);
			this.typs.add(3);
			this.names.add(name);
			++counter;
		}
	}

	public void buttonAddMouseListener(String name, MouseAdapter lis) {
		if (names.contains(name)) {
			int index = names.indexOf(name);
			if (typs.get(index) == 3) {
				this.comps.get(index).addMouseListener(lis);
			}
		}
	}

	public void setAllEnabled(boolean value) {
		for (int i = 0; i < typs.size(); i++) {
			comps.get(i).setEnabled(value);
		}
	}

	public String toString() {
		return "ITableView";
	}
}
